/**
 * Created by 小润 on 2017/3/27.
 */
$(document).ready(function () {
    var ajaxData, shareList, feedId, userId;
    init();

//初始化页面
    function init() {
        $.ajax({
            url: '/api/v1/person/details',
            type: "post",
            async: true,
            success: function (data) {
                ajaxData = data;
                initPage(data);
                console.log(data.result);
            },
            error: function (msg) {
                //alert('请求失败');
                console.log(msg)
            }
        });
    }

    function initPage(data) {
        //接口暂时不可以

        //个人分享列表
        listShare(maxId, pageSize, sinceId, tagId, userId)
    }

//个人分享列表
    function listShare(maxId, pageSize, sinceId, tagId, userId) {
        var data = {
            "maxId": maxId,
            "pageSize": pageSize,
            "sinceId": sinceId,
            "tagId": tagId,
            "userId": userId
        };
        $.ajax({
            type: "get",
            url: '/api/v1/feedService/get/userList',
            data: data,
            success: function (data) {
                shareList = data.data;
                $.each(shareList, function (key, value) {
                    //加载个人分享列表详情
                    var $li = $('<li class="container"></li>');
                    var $row1 = $('<div class="row"></div>');
                    var $row2 = $('<div class="row share-method"></div>');
                    var $rowContent1 = $('<div class="col-xs-6 share-time"></div>' +
                        '<div class="col-xs-6 share-content"></div>');
                    var $rowContent2 = $('<div class="col-xs-6 dianzan"></div>' +
                        '<div class="col-xs-6 comment"></div>');
                    var $share_time = $('<div><span class="share-time-day"></span>' +
                        '<span class="share-time-mouth"></span>月</div>');
                    var $share_content = $('<img src="/images/share-img.png" class="share-img" alt=""/>' +
                        '<span class="share-text">value.content</span>');
                    var $dianzan = $('<img src="/images/dianzan1.png" alt=""/>' +
                        '<span class="dianzan-count">value.likeCount</span>');
                    var $comment = $('<img src="/images/pinglun.png" alt=""/>' +
                        '<span class="pinglun-count">value.commentCount</span>');
                    $('.share-time').append($share_time);
                    $('.share-content').append($share_content);
                    $('.dianzan').append($dianzan);
                    $('.comment').append($comment);
                    $row1.append($rowContent1);
                    $row2.append($rowContent2);
                    $li.append($row1);
                    $li.append($row2);
                    $('#share ul').append($li);
                    //个人分享详情结束
                    feedId = value.feedTag.id;
                });

            },
            error: function (msg) {
                alert('请求失败');
                console.log(msg)
            }
        });
    }

//点击分享图片 
	$(".share_btn").on("click", function() {
        $("#pasonnalPage").hide()
		$("#publish").show()
	});
	 
	$(".cancle_share").on("click",function(){
		$("#pasonnalPage").show()
		$("#publish").hide()		
	})
//点击发送文字分享
	$(".share_btn").on("touchstart",createShare);
	$(".share_btn").on("touchend",function(){
		clearTimeout(timeout);
	});

//点赞or取消赞
    $('.dianzan').click(isLike);

//创建评论
    $('.comment').click(commentEdit);

//查看帖子详情
    $('#share .container').click(shareDetail);


//长按分享文字
	var timeout = undefined;
	function createShare(){
		timeout = setTimeout(function() {
			$("#pasonnalPage").hide()
			$("#publish").show()
			$(".img_box").hide() //图片隐藏
			$(".share_box textarea").css({
				height:"150px"
			})
			
			$(".cancle_share").on("click",function(){
				$("#pasonnalPage").show()
				$("#publish").hide()
				$(".img_box").show() //图片显示
				$(".share_box textarea").css({
					height:"80px"
				})
			})
		}, 1000);
	}
	
	$("#add_more").on("change",handleFiles)
	var aFile;
	var aUrl = [];
	function handleFiles() {
		aFile = this.files
		
		for (var i=0;i<aFile.length;i++) {
			aUrl.push(window.URL.createObjectURL(aFile[i]))			
		}
			console.log(aUrl)
		for (var j=0;j<aUrl.length;j++) {
			$(".img_place").prepend("<li><img src="+aUrl[j]+"></li>")
		}		
	}
	$(".issue_share button").on("click",function(){
		if($(".share_cont").val().trim()==""){
			$(".issue_share button").attr({
				"disabled":false
			})
		}else{
			$("#share ul li").remove()
			$("#pasonnalPage").show()
			$("#publish").hide()
			
			var issMonth,issDay
			var issFont = $(".share_cont").val()
			var data = {
				"issFont":issFont,
				"issMonth":issMonth,
				"issDay":issDay,
			}
			 $.ajax({
                type: "get",
                url: '/api/v1/feedService/get/one',//接口地址？
                data: data,
                success: function (data) {
               		console.log('请求成功');
               		 $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
                +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
                +'<div class="col-xs-6 share-content"><img src="../../images/share-img.png" class="share-img" alt=""/>'
                +'<span class="share-text">'+issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
                +' <img src="../../images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
                +'<img src="../../images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')
                },
                error: function (msg) {
                    console.log('请求失败');
                    console.log(msg)
                }
        	});
			
		}
	})
	
	//分享给家族成员
	$(".shareMem").hide()
	$(".shareTo").click(function(){
		$(".shareMem").slideToggle(500)
	})
	
	//长按亲属删除
	var timer = undefined;
	$(".relation li").on("touchstart",function(){
		oDel = $(this)		
		timer = setTimeout(function(){
			$(".relation_mask,.relation_pop").fadeIn(500)
			$(this).remove()
		},1000)		
	})
	$(".relation li").on("touchend",function(){
		clearTimeout(timer)
	})
	
	$(".relation_cancle").click(function(){
		$(".relation_mask,.relation_pop").fadeOut(500)
	})
	$(".relation_sure").click(function(){
		$(".relation_mask,.relation_pop").fadeOut(500)
		oDel.remove()
		
	})
	
//	从家族成员但中选择
	$(".shareMem").on("click",".applay_people",function(){
		oAdd = $(this)					
		$(".member_mask,.member_pop").fadeIn(500)		
	})
	
	$(".member_cancle").click(function(){
		$(".member_mask,.member_pop").fadeOut(500)
	})
	$(".relation_sure").click(function(){
		$(".member_mask,.member_pop").fadeOut(500)

	})
//分享时获取地理位置
/*$("#locMess").on("click",function(){
	var pos = new BMap.Geolocation()
	pos.getCurrentPosition(function(suc){
		$("#locMess").html("")
		$("#locMess").html(suc.address.street)
	})
})*/

//帖子详情
    function shareDetail(shareList) {
        var index = $(this).index();
        feedId = shareList[index].id;
        var data = {
            "feedId": feedId,
            "userId": userId
        };
        $.ajax({
            type: "get",
            url: '/api/v1/feedService/get/one',
            data: data,
            success: function (data) {

            },
            error: function (msg) {
                alert('请求失败');
                console.log(msg)
            }
        });

    }

//点赞or取消赞
 function isLike(){
        var likeCount = $(this).children('.dianzan-count').text();
        var liked=$(this).children('.liked').val();
        var fid=$(this).children(".fid").val();
        // alert(" liked:"+liked);
        // alert("likecount:"+likeCount);
        // alert("fid:"+fid);
        //var content = $('#editContent').val();
        var obj=$(this)
        var data = {
            /* "customerId": 1,*/
            "id": fid,
            /*"feedId":2,
             "userId":3,*/
           /* "likeCount":likeCount*/
        };
        if (liked==0){
            $.ajax({
                type: "get",
                url: '/api/v1/feedService/like/create',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan.png');
                    alert("点赞成功！");
                    //点赞成功之后把值变成1
                    obj.children(".liked").val(1);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=parseInt(likeCount)+parseInt(1);                   
                    obj.children(".dianzan-count").text(likeCount);
                    //likeCount=likeCount+1;
                    //alert("点数量增加1");

                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
        else{
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/like/delete',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan1.png');
                    alert("取消成功！");
                    obj.children(".liked").val(0);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=likeCount-1;
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
    }


//新增评论
   function commentEdit() {

      var index = $(this).parents("li").index()//点击第几条数据的检索值

	  var obj=$(this)
	  var disCount = $(this).children('.pinglun-count').text();
	  
      $(".cmt_cont").val("")//初始化

      $(".cmt_btm").animate({
         bottom: "0"
      }, 500)

      // 发表评论
      $("#cmt_issue").on("click",function () {

         var toArea = $("#share").children("ul").children("li").eq(index).find(".cmt_area")//添加数据位置
         console.log(toArea)

         var feedId = $("#contId").val()//消息id
         var content = $(".cmt_cont").val()//内容
         var data ={
            "content":content,
            /*"feedId":2*/
            "id":2
//          "disCount":disCount
         }
         
         if(content.trim().length==0 || content==null){

            alert("请输入内容")
            return false;
         }else{
         	
            $.ajax({
               type:"get",
               url:"/api/v1/feedService/comment/create",
               data:data,
               success:function(data){
                  alert("issue success")
                  
              	  obj.children(".pinglun-count").text(" ");
                  disCount=parseInt(disCount)+parseInt(1);
                  obj.children(".pinglun-count").text(disCount);
                  
                  toArea.prepend('<p class="issue_cont"><span class="issue_id">小于</span><span>：</span><span>'+data.data.content+'</span></p >')

					
                  $(".cmt_area").animate({
                     bottom: "-40px"
                  },500)
               },
               error:function(err){
                  alert("issue failed")
               }

            })
         }
      })

      return false
   }
//阻止事件冒泡
    $(".cmt_btm").on("click",function(){
    	return false
    })
    
    $("body").on("click",function(){
		$(".cmt_btm").animate({
    		bottom:"-40px"
    	},500)
	})

   

});

//新增历程
$('.add-process').click(function () {
    $('#pasonnalPage').css('display', 'none');
    $('#addProcess').css('display', 'block');
});
$('#addProcess .go-back').on('click', function () {
    $('#addProcess').css('display', 'none');
    $('#pasonnalPage').css('display', 'block');
});
//跳到新增人物编辑部分
$('.add-editor').on('click', addEditor);
$('#addEditor .go-back').on('click', function () {
    $('#addEditor').css('display', 'none');
    $('#pasonnalPage').css('display', 'block');
    $('.nav li:last').addClass('active').siblings().removeClass('active');
    return false
});
function addEditor() {
    $('#pasonnalPage').css('display', 'none');
    $('#addEditor').css('display', 'block');
}

//添加了遮罩层
$('.more-btn').click(function(){
    $('#zhezhaoceng').show();
    $('#close').show();
});
$('#zhezhaoceng').click(function(){
    $(this).hide();
    $('#close').hide();
});
$('#close').click(function(){
    $('#close').hide();
    $('#zhezhaoceng').hide();
});
//个人页面tab切换
$(".defined_tab li").on("click",function(){
	$(this).addClass("active").siblings().removeClass("active")
	var index = $(this).index()
	$(".tab-content .tab-pane").eq(index).show().siblings().hide()
})


	var oLabel = document.getElementById("oLab")
	console.log(getStyle(oLabel,"width"))
	function getStyle(obj,name){
		if(obj.currentStyle){
			return obj.currentStyle[name];
		}else{
			return getComputedStyle(obj,false)[name];
		}
	}
	
//
$(function(){
	  $("#dropdownMenu").click(function(e) {});
}); 

